<template>
  <div>
    <div class="app-container">
      <div style="padding-bottom: 20px;">
        <el-link type="primary" style="padding-right: 20px;">总数量: {{ this.list.length }}</el-link>
        <el-button
        plain size="small"
          v-waves
          class="filter-item btn"
          type="success"
          icon="el-icon-plus"
          @click="handleAdd()"
        >添加敏感词</el-button>
      </div>
      <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="敏感词" min-width="100" align="center" >
        <template slot-scope="{row}">
          <span>{{ row }}</span>
        </template>
      </el-table-column>
     
      <el-table-column
        label="操作"
        align="center"
        min-width="100"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{row}">
          <!-- <el-button type="primary" plain size="small" @click="handleUpdate(row)">编辑</el-button> -->
          <el-button type="danger" plain size="small" @click="handleDel(row)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>


    </div>

 
<!-- 弹窗 -->
    <el-dialog
      title="sad"
      :visible.sync="updated"
      :width="dialogWidth"
    >
      <el-form
        ref="dataForm"
        :model="temp"
        label-position="right"
        label-width="120px"
        class="fullwidth"
        enctype="multipart/form-data"
      >

        <el-form-item label="敏感词">
          <el-input
            v-model.trim="temp.cut"
            placeholder="敏感词"
          />
          <span>* 多个敏感词可以用","来分隔.</span>
        </el-form-item>

        <div style="text-align: right">
          <el-button plain size="small" @click="updated = false">取消</el-button>
          <el-button
           plain size="small"
            type="primary"
            :loading='addloading'
            @click="handadd()"
          >确定</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getCut, createCut, delCut} from "@/api/adv_admin/config.js"
import waves from "@/directive/waves"; // waves directive
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
import { Message, MessageBox } from "element-ui";
export default {
    'name': 'LandingPage',
    components: { Pagination },
    directives: { waves },
    data() {
        return { 
            imageUrl: '',
            tableKey: 0,
            listLoading: false,
            addloading: false,
            dialogWidth: '0px',
            list: [],
            total: 0,
            listQuery: {
                page: 1,
                limit: 20,
                temp_type: "",
                name: "",
                status: "",
            },
            temp:{
              cut: ''
            },
            updated: false,
            update: '',
            dialogStatus: '',
        }
    },
    mounted(){
        this.getList()
        this.setDialogWidth()
    },
    methods: {
        setDialogWidth() {
            const val = document.body.clientWidth;
            const def = 600; // 默认宽度
            if (val < def) {
                this.dialogWidth = "100%";
            } else {
                this.dialogWidth = def + "px";
            }
        },
        getList(){
            this.listLoading = true
            getCut().then(res => {
                if (res.STATUS == 200){
                    this.list = res.DATA.list
                    console.log(this.bgImgList)
                }
                this.listLoading = false
            })
        },
        handleFilter() {
            this.listQuery.page = 1;
            this.getList();
        },
        handleAdd(){
            this.updated = true
            this.temp.cut = '';
            this.dialogStatus = 'create'
        },
        handadd(){
            this.addloading = true
            this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              createCut(this.temp).then(res => {
                    if (res.STATUS == 200){
                        Message.success('添加成功')
                        this.updated = false
                        this.getList()
                    }else{
                        
                        Message({
                            message: res.DEC,
                            type: "error",
                            duration: 2000,
                        });
                    }
                    this.addloading = false
                })

            } else {
                console.log('error submit!!');
                this.addloading = false
            }
            }); 
        },
        handleDel(item){
            MessageBox.confirm("确定删除吗", "确认", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
              console.log(item,"------------------")
                delCut({ cut: item}).then((r) => {
                    if (r.STATUS == 200) {
                        Message({
                            message: "删除成功",
                            type: "success",
                            duration: 2000,
                        });
                        this.getList();
                    }
                });
            }).catch(() => {});
            
        }
    }
}
</script>
<style lang="scss" scoped>
.show-pwd {
  position: absolute;
  right: 10px;
  top: 0;
  font-size: 16px;
  color: #ccc;
  cursor: pointer;
  user-select: none;
}
.push {
  display: inline-block;
  width: 20px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  margin-left: 5px;
}
.pageHidden {
  display: none;
}
.btn {
  margin-left: 0;
}
.fullwidth {
  width: 450px;
  margin-left: 50px;
}
@media (max-width: 550px) {
  .pageShow {
    display: none;
  }
  .pageHidden {
    display: block;
  }
  .fullwidth {
    width: 100%;
    margin-left: 0;
  }
}

.order_1{
  padding: 2px;
  border-radius: 4px;
  border: 2px solid #409EFF;
}
.order_2{
    padding: 2px;
  border-radius: 4px;
  border: 2px solid #7e7d7d;
}

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 330px;
    /* height: 180px; */
    /* line-height: 178px; */
    text-align: center;
  }
  .avatar {
    width: 330px;
    /* height: 178px; */
    display: block;
  }
  
</style>